#include <cstdio>
#include <pthread.h>
#include <time.h>
#include <unistd.h>

#include <Logger/Static.h>
#include <Timer/Static.h>
#include <Network/Static.h>
#include <Dispatcher/Static.h>
#include <Dispatcher/NSession/State.h>



void openfunc(::Dispatcher::Session *pSession, ::Network::Packet *pPacket)
{
	::Logger::GETLOG()->ToAll(::Logger::DEBUG, "got openfunc!!!!!!!");

	return;
}

void closefunc(::Dispatcher::Session *pSession, ::Network::Packet *pPacket)
{
	::Logger::GETLOG()->ToAll(::Logger::DEBUG, "close openfunc!!!!!!!");

	return;
}


class Init : public ::Dispatcher::NSession::State
{
public:
	Init()
	{
		AddCommand(1, openfunc);
		AddCommand(2, closefunc);
	}
	virtual ~Init()	{}

	virtual std::string	GetName()	{ return "Init"; }
};


class User : public ::Dispatcher::Session
{
public:
	User()
	{
		Init *pInit = new Init;
		this->SetState(pInit);
	}
	virtual ~User()		{}

	void	SetGID(int gid)		{ GID = gid; }
	int		GetGID()			{ return GID; }

protected:
	int		GID;
};

int main(int argc, char **argv)
{
	int port = 10001;

	//
	::Logger::Static logger;
	logger.Init();

	::Timer::Static timer;
	timer.Init();

	::Network::Static network;
	network.Init();

	::Dispatcher::Static dispatcher;
	dispatcher.Init();

	//
	::Timer::Static::InstancePtr()->SetListener(::Dispatcher::GETHANDLER());
	::Network::Static::InstancePtr()->SetListener(::Dispatcher::GETHANDLER());


	//
	::Network::Session::Info info;
	info.eType = ::Network::Session::Info::CLIENT;
	for( int i=0 ; i<100 ; i++ )
	{
		User *user = new User;
		user->SetGID(i);
		user->SetNetworkInfo(info);

		network.GetSessionPool()->Post(port, user);
	}

	//
	if( ::Network::Static::InstancePtr()->Listen(port) )
	{
		::Logger::GETLOG()->ToAll(::Logger::DEBUG, "server listen port = %d", port);
	}
	else
	{
		::Logger::GETLOG()->ToAll(::Logger::DEBUG, "server listen fail");
		return 1;
	}

	while(1)
	{
		sleep(1);
	}



	return 0;
}
